/// <reference path="jquery.js"/>
/// <reference path="jquery.validate.js"/>


jQuery.validator.addMethod("requiredStringEx", 
	function(value, element, param) 
	{
		// check if dependency is met
		if (!this.depend(param, element) )
		{
			return "dependency-mismatch";
		}
		switch( element.nodeName.toLowerCase() ) 
		{
			case 'select':
			{
				var options = jQuery("option:selected", element);
				return options.length > 0 && ( element.type == "select-multiple" || (jQuery.browser.msie && !(options[0].attributes['value'].specified) ? options[0].text : options[0].value).length > 0);
			}
			case 'input':
			{
				if ( this.checkable(element) )
				{
					return this.getLength(value, element) > 0;
				}
			}
			default:
			{
			
				var trimWhiteSpace = GetValue(param, "trimWhiteSpace");
				if (trimWhiteSpace)
				{
					value = jQuery.trim(value);
				}
				var hasInitialValue = GetValue(param, "hasInitialValue");
				if (hasInitialValue)
				{
					var initialValue = GetValue(param, "initialValue");
					var ignoreCase = GetValue(param, "ignoreCase");
					if (ignoreCase)
					{
						value = value.toLowerCase();
						initialValue = initialValue.toLowerCase();
					}
					return initialValue != value;
				}
				else	
				{			
					return value.length > 0;
				}
			}
		}
	}
, "This field is required.");

jQuery.validator.addMethod("requiredNumberEx", 
	function(value, element, param) 
	{
		// check if dependency is met
		if (!this.depend(param, element) )
		{
			return "dependency-mismatch";
		}
		switch( element.nodeName.toLowerCase() ) 
		{
			case 'select':
			{
				var options = jQuery("option:selected", element);
				return options.length > 0 && ( element.type == "select-multiple" || (jQuery.browser.msie && !(options[0].attributes['value'].specified) ? options[0].text : options[0].value).length > 0);
			}
			case 'input':
			{
				if ( this.checkable(element) )
				{
					return this.getLength(value, element) > 0;
				}
			}
			default:
			{
				value = jQuery.trim(value);
				var allowDecimalPoint = GetValue(param, "allowDecimalPoint");
				if (!allowDecimalPoint)
				{
					if (parseFloat(value) != parseInt(value))
					{
						return false;
					}
				}
				value = parseFloat(value);
				if (!value)
				{
					return false;
				}
				
				var hasInitialValue = parseFloat(GetValue(param, "hasInitialValue"));
				var initialValue = 0;
				if (hasInitialValue)
				{
					var initialValue = GetValue(param, "initialValue");
				}
				return initialValue != value;
			}
		}
	}
, "This field is required.");


jQuery.validator.addMethod("rangeNumberEx", 
	function( value, element, param ) 
	{
		
		if (this.optional(element))
		{
			return true;
		}
		else
		{
			var equalsMaximumIsValid = GetValue(param, "equalsMaximumIsValid");
			var equalsMinimumIsValid = GetValue(param, "equalsMinimumIsValid");
			var maximum = parseFloat(GetValue(param, "maximum"));
			var minimum = parseFloat(GetValue(param, "minimum"));
			var allowDecimalPoint = GetValue(param, "allowDecimalPoint");
			if (!allowDecimalPoint)
			{
				if (parseFloat(value) != parseInt(value))
				{
					return false;
				}
			}
			value = parseFloat(value);
			if (!value)
			{
				return false;
			}
			if (equalsMinimumIsValid && equalsMaximumIsValid)
			{			
				return ( value >= minimum && value <= maximum );
			}
			else if(equalsMinimumIsValid && !equalsMaximumIsValid)
			{			
				return ( value >= minimum && value < maximum );
			}
			else if(!equalsMinimumIsValid && equalsMaximumIsValid)
			{			
				return ( value > minimum && value <= maximum );
			}
			else 
			{			
				return ( value > minimum && value < maximum );
			}
		}
	}
, jQuery.format("Please enter a value between {0} and {1}."));



var CompareOperator = 
{
        Equal:1,
        GreaterThan:2,
        GreaterThanEqual:3,
        LessThan:4,
        LessThanEqual:5,
        NotEqual:6
  };





function Contains(array, string)
{
	return array.indexOf(string) != -1;
}

function GetValue(array, key)
{
	var value = false;
	for (var x in array)
	{
		var keyValue = array[x].toString();
		var index = keyValue.indexOf("=");
		if (keyValue == key)
		{
			value = true;
			break;
		}
		if (index != -1)
		{
			var splitArray = keyValue.split("=");
			var tempKey = splitArray[0];
			if (tempKey == key)
			{
				value = splitArray[1];
				break;
			}
		}
	}
	if (value == 'true')
	{
		return true;
	}
	else if (value == 'false')
	{
		return false;
	}
	else
	{
		return value;
	}
}
